//
// Created by Asus on 2025/5/12.
//

#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <vector>
#include <bits/ostream.tcc>
/* 基本的内置类型 */
void base_type() {
    bool bool_val{true};
    std::cout << "bool_val:" << bool_val << std::endl;
    char char_val{'a'};
    std::cout << "char_val:" << char_val << std::endl;
    int int_val{42};
    std::cout << "int_val:" << int_val << std::endl;
    float float_val{3.14f};
    std::cout << "float_val:" << float_val << std::endl;
    double double_val{3.14};
    std::cout << "double_val:" << double_val << std::endl;
    void *void_val{nullptr};
    std::cout << "void_val:" << void_val << std::endl;
    void *void_val2;
    std::cout << "void_val2:" << void_val2 << std::endl;
    const wchar_t *wchar_val2 = L"<UNK>"; // 正确
    std::cout << "wchar_val2:" << wchar_val2 << std::endl;

    typedef short int my_wchar_t;
    my_wchar_t my_wchar_t_val{21};
    std::cout << "my_wchar_t_val:" << my_wchar_t_val << std::endl;
    const my_wchar_t *my_wchar_val2 = reinterpret_cast<const my_wchar_t *>(L"<UNK>");
    std::cout << "my_wchar_val2:" << my_wchar_val2 << std::endl;
}

class Counter {
public:
    Counter() : count_(0) {
    }

    void increment() const {
        count_++;
    }

    int get_count() const {
        return count_;
    }

private:
    mutable int count_;
};

/* 基本的内置类型2 */
void base_type2() {
    signed int signed_int_val = -10;
    std::cout << "signed_int_val:" << signed_int_val << std::endl;
    unsigned int unsigned_int_val = -1;
    std::cout << "unsigned_int_val:" << unsigned_int_val << std::endl;
    short short_val = 10;
    std::cout << "short_val:" << short_val << std::endl;
    long long_val = 10;
    std::cout << "long_val:" << long_val << std::endl;
    const int int_val = 10;
    std::cout << "int_val:" << int_val << std::endl;
    // int_val = 20; // 报错
    volatile int volatile_val = 10;
    std::cout << "volatile_val:" << volatile_val << std::endl;
    volatile_val = 11;
    std::cout << "volatile_val:" << volatile_val << std::endl;

    const Counter counter;
    std::cout << "counter count:" << counter.get_count() << std::endl;
    counter.increment();
    counter.increment();
    std::cout << "counter count:" << counter.get_count() << std::endl;
}

void base_type3() {
    bool bool_val{true};
    std::cout << "bool_val:" << bool_val << std::endl;
    char char_val{'a'};
    std::cout << "char_val:" << char_val << std::endl;
    signed char schar_val = 'a';
    std::cout << "schar_val:" << schar_val << std::endl;
    unsigned char uchar_val = 'a';
    std::cout << "uchar_val:" << uchar_val << std::endl;
    wchar_t wchar_val3 = 10;
    std::cout << "wchar_val3:" << wchar_val3 << std::endl;
    char16_t char16_val = 10;
    std::cout << "char16_val:" << char16_val << std::endl;
    char32_t char32_val = 10;
    std::cout << "char32_val:" << char32_val << std::endl;
    short short_val = 10;
    std::cout << "short_val:" << short_val << std::endl;
    unsigned short ushort_val = 10;
    std::cout << "ushort_val:" << ushort_val << std::endl;
    int int_val = 10;
    std::cout << "int_val:" << int_val << std::endl;
    unsigned int unsigned_int_val = 10;
    std::cout << "unsigned_int_val:" << unsigned_int_val << std::endl;
    long long_val = 10;
    std::cout << "long_val:" << long_val << std::endl;
    unsigned long ulong_val = 10;
    std::cout << "ulong_val:" << ulong_val << std::endl;
    long long long_long_val = 10;
    std::cout << "long_long_val:" << long_long_val << std::endl;
    unsigned long long ulong_long_val = 10;
    std::cout << "ulong_long_val:" << ulong_long_val << std::endl;
    float float_val = 10.0;
    double double_val = 10.0;
    std::cout << "float_val:" << float_val << std::endl;
    std::cout << "double_val:" << double_val << std::endl;
    long double long_double_val = 10.0;
    std::cout << "long_double_val:" << long_double_val << std::endl;
}

void c11_add_type() {
    auto auto_val = 3.14f;
    auto *auto_void_val = &auto_val;
    std::cout << "auto_val:" << auto_val << std::endl;
    decltype(auto_val) auto_val2 = 20;
    std::cout << "auto_val2:" << auto_val2 << std::endl;
    int *ptr_val = nullptr;
    std::cout << "ptr_val:" << ptr_val << std::endl;

    std::initializer_list<int> init_list{1, 2, 3, 4, 5};
    std::cout << "init_list:[";
    for (int int_val: init_list) {
        std::cout << int_val << ",";
    }
    std::cout << "]" << std::endl;
    std::initializer_list<std::string> str_list = {"a", "b", "c"};
    std::cout << "str_list:[";
    for (std::string string_val: str_list) {
        std::cout << string_val << ",";
    }
    std::cout << "]" << std::endl;

    std::tuple<int, float, char> tuple_val(1, 2.0, 'a');
    std::cout << "tuple_val:" << std::get<0>(tuple_val) << std::endl;
    std::cout << "tuple_val:" << std::get<1>(tuple_val) << std::endl;
    std::cout << "tuple_val:" << std::get<2>(tuple_val) << std::endl;

    std::vector<int> int_vec(init_list);
    int_vec[0] = 101;
    std::cout << "int_vec[0]:" << int_vec[0] << std::endl;
}

int func(int num1, int num2) {
    return num1 + num2;
}

struct Point {
    int x;
    int y;
};

typedef Point *point;

using my_point = Point;

using my_point2 = struct Point2 {
    int x;
    int y;
};

typedef struct Point3 {
    int x;
    int y;
    int z;
} my_point3;

class MyClass {

};



union Data {
    int a;
    int b;
};

typedef enum Color {
    GREEN,
    RED
} my_color;

using my_color2 = enum Color2 {
    YELLOW,
    BLUE
};

void my_point_func() {
    my_point3 point = {1, 2, 3,};
    std::cout << "point.x:" << point.x << "point.y" << point.x << "point.z" << point.z << std::endl;
    Color color = Color::GREEN;
    std::cout << "color:" << color << std::endl;
}

void derive_type() {
    int arr[5]{1, 2, 3, 4, 5};
    int arr2[5] = {1, 2, 3, 4, 5};
    arr[0] = 10;
    std::cout << "arr[0]:" << arr[0] << std::endl;
    int x = 10;
    int *ptr = &x;
    int &ref = x;
    std::cout << "ptr:" << ptr << std::endl;
    std::cout << "ref:" << ref << std::endl;
    std::cout << "*ptr:" << *ptr << std::endl;
    std::cout << "&ref:" << &ref << std::endl;
    func(1, 2);
}

void standard_lib_type() {
    std::string str = "Hello World!";
    std::cout << "str:" << str << std::endl;
    std::vector<int> vec = {1, 2, 3, 4, 5};
    std::pair<int, int> pair = {1, 2};
    std::cout << "pair.first: " << pair.first << " pair.second:" << pair.second << std::endl;
    std::map<int, int> map = {{1, 2}, {3, 4}};
    for (auto it = map.begin(); it != map.end(); it++) {
        std::cout << it->first << ":" << it->second << std::endl;
    }
    std::set<int> set1{1, 2,};
    for (auto it = set1.begin(); it != set1.end(); it++) {
        std::cout << *it << std::endl;
    }

}

class Base {
public:
    virtual ~Base() = default;
};

class Derived : public Base {
public:
    void show() {
        std::cout << "Derived::show()" << std::endl;
    }
};

class DerivedSub : public Derived {
public:
    void show() {
        std::cout << "DerivedSbu::show()" << std::endl;
    }
};


/* 转换 */
void cast() {
    int i = 10;
    float x = static_cast<float>(i);
    // 静态转换是将一种数据类型的值强制转换为另一种数据类型的值。
    std::cout << "i:" << i << std::endl;
    std::cout << "x:" << typeid(x).name() << std::endl;
    int num = 10;
    short sthort_val = (short)num;
    short sshort_val = 1;
    int num2 = (int) sshort_val;
    std::cout << "num2:" << num2 << std::endl;
    std::cout << "sthort_val:" << sthort_val << std::endl;
    // 指针类型的动态转换
    Base *ptr_base = new Derived();
    Derived *ptr_derived = dynamic_cast<Derived *>(ptr_base);
    if (ptr_derived) {
        ptr_derived->show();
    } else {
        std::cout << "cast faild!" << std::endl;
    }
    delete ptr_derived;
    // 连续转换
    Base *ptr_base2 = new DerivedSub();
    DerivedSub *ptr_derived2 = dynamic_cast<DerivedSub *>(dynamic_cast<Derived *>(ptr_base2));
    if (ptr_derived2) {
        ptr_derived2->show();
    } else {
        std::cout << "cast faild!" << std::endl;
    }
    // 引用类型的动态转换
    DerivedSub derived_sub;
    Base& ref_base = derived_sub;
    try {
        DerivedSub &ref_derived_sub = dynamic_cast<DerivedSub &>(dynamic_cast<Derived &>(ref_base));
        ref_derived_sub.show();
    } catch (const std::bad_cast &e) {
        std::cout << "Dynamic cast failed:" << e.what() << std::endl;
    }
    // 常量转换
    const int int_val = 10;
    int& ref_int = const_cast<int&>(int_val);
    std::cout << "int_val:" << int_val << std::endl;
    // 重新解释转换
    int int_val2 = 10;
    float float_val = reinterpret_cast<float&>(int_val2);
    std::cout << "float_val:" << float_val << std::endl;
}

int main(char *argv[]) {
    // 基本内置类型
    // base_type();
    // base_type2();
    // base_type3();
    // c11新增类型
    // c11_add_type();
    // 派生数据类型
    // derive_type();
    // my_point_func();
    // 标准库类型
    // standard_lib_type();
    // 类型转换
    cast();
}
